//
//  AlbumViewController.m
//  MIXMT
//
//  Created by 方希罕 on 16/5/19.
//  Copyright © 2016年 FGL. All rights reserved.
//

#import "AlbumViewController.h"
#import "PHKitTool.h"
//#import "CameraViewController.h"
#import "AlbumView.h"
#import "MainTabBarController.h"

@interface AlbumViewController ()

<
    AlbumViewDelegate
>

@end

@implementation AlbumViewController

- (void)viewWillAppear:(BOOL)animated{
    
    self.imageArray = [MainTabBarController shareImageArray];
    
}
- (void)requestAuthorizationStatus
{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
        dispatch_async(dispatch_get_main_queue(), ^{
            switch (status) {
                case PHAuthorizationStatusAuthorized:
                {
                    [self checkAuthorizationSuccess];
                    break;
                }
                default:
                {
                    [self showAccessDenied];
                    break;
                }
            }
        });
    }];
}

- (void)checkAuthorizationSuccess{
    AlbumView *albumView = [[AlbumView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
    albumView.imageArray = self.imageArray;
    [albumView.albumCollectionView reloadData];
    albumView.delegate = self;
    albumView.allowSelectCount = self.allowSelectCount;
    [self.view addSubview:albumView];
//    NSLog(@"ok");
}

- (void)showAccessDenied{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    label.text = @"请先打开相册的访问权限";
    [label.font fontWithSize:25];
    label.textAlignment = 1;
    label.textColor = [UIColor whiteColor];
    [self.view addSubview:label];
//    NSLog(@"no");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestAuthorizationStatus];

    self.title = @"相机胶卷";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createButton];

    
}

//- (void)sendAsset:(PHAsset *)asset pixWidth:(NSInteger)pixWidth pixHeight:(NSInteger)pixHeight {
//
//    BeautifyPictureViewController *bpVC = [[BeautifyPictureViewController alloc] init];
//    bpVC.asset = asset;
//    bpVC.pixWidth = pixWidth;
//    bpVC.pixHeight = pixHeight;
//    [self.navigationController pushViewController:bpVC animated:YES];
//    
////    NSLog(@"vc == %@", image);
//
//}

- (void)createButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"xiangji.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, kScreenHeight - 88, kScreenWidth, 44);
    [self.view addSubview:button];
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:@selector(goToCamera:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)goToCamera:(UIButton *)button{
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//        CameraViewController *cameraVC = [[CameraViewController alloc]init];
//        [self.navigationController pushViewController:cameraVC animated:NO];

//        UIImagePickerController *picker=[[UIImagePickerController alloc]init];
//        picker.delegate=self;
//        picker.allowsEditing=YES;
//        picker.sourceType=UIImagePickerControllerSourceTypeCamera;
//        [self presentViewController:picker animated:NO completion:^{
//            
//        }];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示:" message:@"此设备没有摄像头." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:sure];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    
}



- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
